/**
 * 账号表模型
 */
module.exports = (app) => {
  const { STRING, BIGINT, BOOLEAN, DATE, DOUBLE } = app.Sequelize
  const Account = app.model.define('account', {
    id: {
      type: BIGINT(20),
      autoIncrement: true,
      primaryKey: true,
    },
    userId: {
      type: BIGINT(20),
      allowNull: true,
    },
    openCode: {
      type: STRING,
      allowNull: true,
    },
    category: {
      type: BOOLEAN,
      allowNull: true,
    },
    created: {
      type: DATE,
      allowNull: true,
      defaultValue: app.Sequelize.NOW
    },
    creator: {
      type: STRING(36),
      allowNull: true,
    },
    edited: {
      type: DATE,
      allowNull: true,
    },
    editor: {
      type: STRING(36),
      allowNull: true,
    },
    deleted: {
      type: DOUBLE,
      allowNull: true,
    },
  },
    {
      freezeTableName: true, // Model 对应的表名将与model名相同
      timestamps: false,
    })

  // 表关联的字段
  Account.associate = function () {
    /**
   * Account.belongsTo(关联的模型, { foreignKey: '使用什么字段关联', targetKey: '与关联的模型那个字段关联', as: '别名' });
   */
    // 一对一
    app.model.Account.belongsTo(app.model.User, {
      foreignKey: 'userId',
      targetKey: 'id',
      as: 'user',
    })
  }

  return Account
}